package cn.com.gaobb.service.impl;

import cn.com.gaobb.dto.ResultDTO;
import cn.com.gaobb.entity.TTruck;
import cn.com.gaobb.mapper.TTruckMapper;
import cn.com.gaobb.service.TruckService;
import cn.com.gaobb.utils.BeanHelper;
import cn.com.gaobb.utils.IdBuilderUtil;
import cn.com.gaobb.vo.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

@Service
public class TruckServiceImpl implements TruckService {
    @Autowired
    private TTruckMapper TruckMapper;
    @Override
    public ResultDTO<ResIdVO> add(ReqTruckAddVO reqVO) {
        TTruck tTruck = BeanHelper.copyProperties(reqVO, TTruck.class);
        tTruck.setId(IdBuilderUtil.getId());
        tTruck.setCreateTime(new Date());
        tTruck.setModifyTime(new Date());
        TruckMapper.insertSelective(tTruck);
        return ResultDTO.okOf(new ResIdVO(tTruck.getId()));
    }

    @Override
    public ResultDTO update(ReqTruckUpdateVO vo) {
        TTruck tTruck = BeanHelper.copyProperties(vo, TTruck.class);
        tTruck.setModifyTime(new Date());
        TruckMapper.updateByPrimaryKeySelective(tTruck);
        return ResultDTO.okOf();
    }

    @Override
    public ResultDTO deleteList(ReqTruckDeleteListVO vo) {
        if(CollectionUtils.isEmpty(vo.getIdList())){
            return ResultDTO.errorOf(1001, "请选择要删除的客户");
        }
        vo.getIdList().forEach(id->{
            TruckMapper.deleteByPrimaryKey(id);
        });
        return ResultDTO.okOf();
    }

    @Override
    public ResultDTO<ResTruckDetailVO> detail(String id) {
        TTruck tTruck = TruckMapper.selectByPrimaryKey(id);
        ResTruckDetailVO resTruckDetailVO = BeanHelper.copyProperties(tTruck, ResTruckDetailVO.class);
        return ResultDTO.okOf(resTruckDetailVO);
    }

    @Override
    public ResultDTO<ResPageVO<ResTruckDetailVO>> listOfPage(ReqPageVO vo) {
        PageHelper.startPage(vo.getCurrentPage(), vo.getPageSize());
        List<ResTruckDetailVO> list =  TruckMapper.list(vo.getSearchParem());
        PageInfo<ResTruckDetailVO> pageInfo = new PageInfo<>(list);
        ResPageVO<ResTruckDetailVO> resPageVO = new ResPageVO<>();
        resPageVO.setRows(pageInfo.getList());
        resPageVO.setCurrentPage(pageInfo.getPageNum());
        resPageVO.setTotal((int) pageInfo.getTotal());
        return ResultDTO.okOf(resPageVO);
    }
}
